package com.fancymusic.controller;

import com.fancymusic.entity.BaseResult;
import com.fancymusic.entity.Singer;
import com.fancymusic.service.ISingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("singer")
public class SingerController {


    @Autowired
    ISingerService iSingerService;

    @RequestMapping("getSingerByName")
    public BaseResult<Singer> getSingleByName(@RequestBody Singer singer) {

        BaseResult<Singer> result = new BaseResult<>();

        if (singer == null) {
            result.setMessage("歌手信息输入错误");
            return result;
        }


        List<Singer> singerByname = iSingerService.getSingerByname(singer);

        if (singerByname == null) {
            result.setMessage("歌手不存在");
            return result;
        }

        result.setList(singerByname);
        result.setMessage("歌手信息已找到");

        return result;
    }


    @RequestMapping("getSingerList")
    public BaseResult<Singer> getSingerList() {

        BaseResult<Singer> result = new BaseResult<>();

        List<Singer> singerList = iSingerService.getSingerList();

        result.setList(singerList);
        result.setMessage("歌手信息已找到");

        return result;
    }

    @RequestMapping("save")
    public BaseResult<Singer> save(@RequestBody Singer singer) {

        BaseResult<Singer> result = new BaseResult<>();

        iSingerService.saveSinger(singer);

        result.setMessage("歌手信息已保存");

        return result;
    }

    /**
     *  dynamic sql
     *
     * @param
     * @return
     */
    @RequestMapping("getSingerDynamic")
    public BaseResult<Singer> save(@RequestBody Map map) {

        System.out.println("===前台传入的map为：======"+map);
        BaseResult<Singer> result = new BaseResult<>();


        List<Singer> singerDynamic = iSingerService.findSingerDynamic(map);

        result.setList(singerDynamic);
        result.setMessage("歌手信息已保存");

        return result;
    }


    @RequestMapping("getSingerAdress")
    public BaseResult<String> getSingerAdress() {

        BaseResult<String> result = new BaseResult<>();


        List<String> allRegin = iSingerService.getAllRegin();

        result.setList(allRegin);

        return result;
    }
}
